<template>
  <div class="mdui-container">
    <br>
    <h2>分类添加</h2>
    <div class="mdui-textfield">
      <label class="mdui-textfield-label">分类名称</label>
      <input class="mdui-textfield-input" type="text" v-model="name"/>
    </div>
    <div class="mdui-textfield">
      <label class="mdui-textfield-label">分类权重(默认100)</label>
      <input class="mdui-textfield-input" type="text" v-model="weight"/>
    </div>
    <br>
    <br>
    <button class="mdui-btn mdui-float-right" v-on:click="add">添加</button>
  </div>
</template>

<script>
  import * as mdui from "mdui";
  import axios from "axios";
  import qs from "qs";
  import jm from "./../../static/jm"

  const BASE_URL = 'http://127.0.0.1:8886';
  export default {
    name: 'login',
    data () {
      return {
        phone_number: '',
        password: '',
        name:'',
        weight:'',
        sort_id:''
      }
    },
    methods:{
      login:function(){



      },
      add(){
        if(!this.name)
        {
          mdui.default.alert("请输入名称");
          return;
        }
        if(!this.weight)
        {
          mdui.default.alert("请输入权重");
          return;
        }
        this.token=window.localStorage.getItem("token");
        axios.post(BASE_URL +'/sort/add_sort',
          qs.stringify({token:this.token,name:this.name,weight:this.weight}),
          {'headers': {'Content-Type': 'application/x-www-form-urlencoded'}})
          .then(re => {
            if(re.data.状态码===200)
            {
              this.$router.push("/sort_list");

            }
            else
            {
              if(re.data.备注==='token无效') {
                this.token = window.localStorage.setItem("token", "");
                this.$router.push("/login");
              }
              else
                mdui.default.alert(re.data.备注);
            }
          })
          .catch(err=>{
            mdui.default.alert("网络错误");

          });


      }
    },
    mounted() {

    }
  }
</script>
